package com.douxg.learning.springboot.base.youxi.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.douxg.learning.springboot.base.youxi.serrvice.ShuiPaiXuService;
import com.douxg.learning.springboot.base.youxi.shuipaixu.Cup;
import com.douxg.learning.springboot.base.youxi.shuipaixu.ShuiPaiXu;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description 水排序谜题，按照颜色将水分类
 * @Auther douxg
 * @Date 2022/9/17 14:04
 */
@RestController
@RequestMapping("youxi/shuipaixu")
public class ShuiPaiXuController {
    @PutMapping
    public String run(@RequestBody ShuiPaiXu shuiPaiXu) throws InterruptedException {
        String s = checkCups(shuiPaiXu);
        if (s != null) {
            return s;
        }
        ShuiPaiXuService shuiPaiXuService = new ShuiPaiXuService();
        shuiPaiXuService.setShuiPaiXu(shuiPaiXu);
        shuiPaiXuService.start();
        while (shuiPaiXuService.isAlive()) {
            TimeUnit.SECONDS.sleep(1);
        }
        return StrUtil.join("\n", shuiPaiXu.getResult());
    }

    @PostMapping("")
    public String run(@RequestBody List<String> list) throws InterruptedException {
        List<Cup> cups = new ArrayList<>();
        for (String s : list) {
            if (StrUtil.isBlank(s)) {
                cups.add(new Cup().setThings(new ArrayList<>()));
            } else {
                cups.add(new Cup().setThings(CollectionUtil.newArrayList(s.split(","))));
            }
        }
        ShuiPaiXu shuiPaiXu = new ShuiPaiXu();
        shuiPaiXu.setCupCapacity(4);
        shuiPaiXu.setCups(cups);
        return run(shuiPaiXu);
    }

    @GetMapping
    public String run(@RequestParam("cups") String cups) throws InterruptedException {
        String[] list = cups.split(";");
        return run(CollectionUtil.newArrayList(list));
    }

    private String checkCups(ShuiPaiXu shuiPaiXu) {
        List<Cup> cups = shuiPaiXu.getCups();
        Map<String, Integer> map = new HashMap<>();
        for (Cup cup : cups) {
            List<String> things = cup.getThings();
            if (things == null) {
                continue;
            }
            for (String thing : things) {
                Integer integer = map.get(thing);
                if (integer == null) {
                    integer = 0;
                }
                map.put(thing, integer + 1);
            }
        }
        int i = 0;
        int cupCapacity = 0;
        String firstKey = "";
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (i == 0) {
                cupCapacity = entry.getValue();
                firstKey = entry.getKey();
                i++;
                continue;
            }
            if (entry.getValue() != cupCapacity) {
                return "检测到东西：" + entry.getKey() + ":" + entry.getValue() + "或者" + firstKey + ":" + cupCapacity + "数量不一致";
            }
        }
        return null;
    }
}
